package com.java.comprehensive.stream;

import java.util.*;
import java.util.function.*;
import java.util.stream.Stream;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @program: comprehensive-service
 *
 * @description:
 *
 * @author: 窦绍飞
 *
 * @create: 2021-12-08 15:14
 **/
@SpringBootTest
public class StreamTestOne {

	@Test
	public void testOne() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("你知道吗 我比你想象的 更想在你身边");
			}
		}).start();
		new Thread(() -> {
			System.out.println("你知道吗 我比你想象的 更想在你身边");
		}).start();
	}

	public static int calculateNum(IntBinaryOperator operator) {
		int a = 10;
		int b = 20;
		return operator.applyAsInt(a, b);
	}

	// public static void main(String[] args) {
	// /* int i = calculateNum(new IntBinaryOperator() {
	// @Override
	// public int applyAsInt(int left, int right) {
	// return left + right;
	// }
	// });
	// System.out.println(i);*/
	//
	// int i = calculateNum(Integer::sum);
	// System.out.println(i);
	// }

	// public static void main(String[] args) {
	// printNum((int value) -> {
	// return value % 2 == 0;
	// });
	// }

	public static void printNum(IntPredicate predicate) {
		int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		for (int i : arr) {
			if (predicate.test(i)) {
				System.out.println(i);
			}
		}
	}

	private static List<Author> getAuthors() {
		// 数据初始化
		Author author = new Author(1L, "蒙多", 58, "一个从菜刀中明悟哲理的祖安人", null);
		Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上他的思考速度", null);
		Author author3 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);
		Author author4 = new Author(3L, "易", 14, "是这个世界在限制他的思维", null);

		// 书籍列表
		List<Book> books1 = new ArrayList<>();
		List<Book> books2 = new ArrayList<>();
		List<Book> books3 = new ArrayList<>();

		books1.add(new Book(1L, "刀的两侧是光明与黑暗", "哲学,爱情", 88, "用一把刀划分了爱恨"));
		books1.add(new Book(2L, "一个人不能死在同一把刀下", "个人成长,爱情", 99, "讲述如何从失败中明悟真理"));

		books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
		books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85, "带你用思维去领略世界的尽头"));
		books2.add(new Book(4L, "吹或不吹", "爱情,个人传记", 56, "一个哲学家的恋爱观注定很难把他所在的时代理解"));

		books3.add(new Book(5L, "你的剑就是我的剑", "爱情", 56, "无法想象一个武者能对他的伴侣这么的宽容"));
		books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));
		books3.add(new Book(6L, "风与剑", "个人传记", 100, "两个哲学家灵魂和肉体的碰撞会激起怎么样的火花呢？"));

		author.setBooks(books1);
		author2.setBooks(books2);
		author3.setBooks(books3);
		author4.setBooks(books3);

		return new ArrayList<>(Arrays.asList(author, author2, author3, author4));
	}

	public static void main(String[] args) {
		// 打印所有年龄小于18的作家的名字，并且要注意去重
		// List<Author> authors = getAuthors();
		// authors.stream()// 把集合转换成流
		// .distinct()// 先去除重复的作家
		// .filter(author -> author.getAge() < 18)// 筛选年龄小于18的
		// .forEach(author -> System.out.println(author.getName()));// 遍历打印名字
		//
		// Author author = new Author();
		// Optional<Author> authorOptional = Optional.of(author);
		// Author author1 = authorOptional.orElseGet(Author::new);

		// Optional<Author> authorOptional = Optional.ofNullable(new Author());
		// try {
		// Author author = authorOptional.orElseThrow((Supplier<Throwable>) () -> new
		// RuntimeException("author为空"));
		// System.out.println(author.getName());
		// } catch (Throwable throwable) {
		// throwable.printStackTrace();
		// }

		/* List<Author> authors = getAuthors(); Stream<Author> authorStream =
		 * authors.stream(); authorStream.filter(new Predicate<Author>() {
		 * 
		 * @Override public boolean test(Author author) { return author.getAge()>17; }
		 * }.and(new Predicate<Author>() {
		 * 
		 * @Override public boolean test(Author author) { return
		 * author.getName().length()>1; } })).forEach(System.out::println); } */
//		List<Author> authors = getAuthors();
//		authors.stream().filter(new Predicate<Author>() {
//			@Override
//			public boolean test(Author author) {
//				return author.getAge() > 17;
//			}
//		}.or(new Predicate<Author>() {
//			@Override
//			public boolean test(Author author) {
//				return author.getName().length() < 2;
//			}
//		})).forEach(author -> System.out.println(author.getName()));
		List<Author> authors = getAuthors();
		JSON parse = JSONUtil.parse(authors);
		System.out.println(parse.toString());
	}

	private static void test28() {
		Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		Integer sum = stream.parallel()
			.peek(new Consumer<Integer>() {
				@Override
				public void accept(Integer num) {
					System.out.println(num+Thread.currentThread().getName());
				}
			})
			.filter(num -> num > 5)
			.reduce((result, ele) -> result + ele)
			.get();
		System.out.println(sum);
	}
}
